using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using webControls = System.Web.UI.WebControls;
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Inventory;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace CatalogManagerDAL
{
    public class UpdateEntities
    {
        private ListEntities DAL_ListEntities = new ListEntities();
        private CatalogContext CommerceCtx = DataHelper.InitContext();

        public Dictionary<string, webControls.WebControl> CreateControls_ForProduct(string EntityName)
        {
            Dictionary<string, webControls.WebControl> ControlsList = new Dictionary<string, webControls.WebControl>();

            #region DefinitionProperties table is begin prepared
            DataTable DefinitionProperties = DAL_ListEntities.ListSingleProductDefinitionsProperties(EntityName);
            DefinitionProperties.Columns.Add("PropertyDataType", Type.GetType("System.String"));
            DefinitionProperties.Columns.Add("PropertyDisplayName",  Type.GetType("System.String"));
            DefinitionProperties.Columns.Add("IsRequired", Type.GetType("System.Boolean"));
            
            foreach (DataRow DefinitionPropertyRow in DefinitionProperties.Rows)
            {
                // object[] tmp = DefinitionPropertyRow.ItemArray;
                string PropertyName = DefinitionPropertyRow["PropertyName"].ToString();
                DataRow PropertyTypeRow = DAL_ListEntities.GetProductDefinitionPropertyAttributes(
                    String.Format("PropertyName='{0}'", PropertyName)
                );
                DefinitionPropertyRow["PropertyDataType"] = Convert.ToInt32(PropertyTypeRow["DataType"]);
                DefinitionPropertyRow["PropertyDisplayName"] = Convert.ToString(PropertyTypeRow["DisplayName_en-US"]);
                DefinitionPropertyRow["IsRequired"] = Convert.ToBoolean(PropertyTypeRow["IsRequired"]);
            }
            DefinitionProperties.DefaultView.Sort = "PropertyDisplayName";
            DefinitionProperties = DefinitionProperties.DefaultView.ToTable();

            #endregion

            foreach (DataRow DefinitionPropertyRow in DefinitionProperties.Rows)
            {
                DataHelper.PropertyDataType propertyDataType = (DataHelper.PropertyDataType) Convert.ToInt32(DefinitionPropertyRow["PropertyDataType"]);
                string PropertyDefinitionName = Convert.ToString(DefinitionPropertyRow["PropertyName"]);
                string PropertyDisplayName = Convert.ToString(DefinitionPropertyRow["PropertyDisplayName"]);
                bool isRequired = Convert.ToBoolean(DefinitionPropertyRow["IsRequired"]);
                
                
                string PropertyName = PropertyDisplayName + " - " + PropertyDefinitionName;
                string ControlIdPrefix = "o_";
                if (isRequired) ControlIdPrefix = "r_";

                #region Create the control and add it to the control array

                switch (propertyDataType)
                {
                    case DataHelper.PropertyDataType.Integer:
                    case DataHelper.PropertyDataType.BigInteger:
                    case DataHelper.PropertyDataType.Float:
                    case DataHelper.PropertyDataType.Double:
                    case DataHelper.PropertyDataType.String:
                    case DataHelper.PropertyDataType.Text:
                    case DataHelper.PropertyDataType.DateTime:
                    case DataHelper.PropertyDataType.Date:
                    case DataHelper.PropertyDataType.Time:
                        webControls.TextBox txtTmp = new webControls.TextBox();
                        txtTmp.ID = ControlIdPrefix + PropertyDefinitionName;
                        ControlsList.Add(PropertyName, txtTmp);
                        break;
                    case DataHelper.PropertyDataType.Boolean:
                        webControls.CheckBox chkTmp = new webControls.CheckBox();
                        chkTmp.ID = ControlIdPrefix + PropertyDefinitionName;
                        ControlsList.Add(PropertyName, chkTmp);
                        break;
                    case DataHelper.PropertyDataType.Enumeration:
                        webControls.DropDownList ddlTmp = new webControls.DropDownList();
                        ddlTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        CatalogEnumerationProperty oCatalogProperty = (CatalogEnumerationProperty) CommerceCtx.GetProperty(PropertyDefinitionName);
                        CatalogEnumerationPropertyValuesDataSet.EnumerationPropertyValuesDataTable oEnumerationPropertyValues = oCatalogProperty.EnumerationValues.EnumerationPropertyValues;
                        oEnumerationPropertyValues.DefaultView.Sort = "PropertyDisplayName";
                        foreach (DataRow enumValue in oEnumerationPropertyValues.DefaultView.ToTable().Rows)
                        {
                            ddlTmp.Items.Add(enumValue["PropertyValue"] as String);
                        }

                        ControlsList.Add(PropertyName, ddlTmp);
                        break;
                    case DataHelper.PropertyDataType.Currency:
                        break;
                    case DataHelper.PropertyDataType.FilePath:
                        break;
                    case DataHelper.PropertyDataType.MultiValue:
                        break;
                    case DataHelper.PropertyDataType.CallbackEvent:
                        break;
                    case DataHelper.PropertyDataType.MultilingualEnumeration:
                        break;
                    case DataHelper.PropertyDataType.Guid:
                        break;
                    case DataHelper.PropertyDataType.Custom1:
                        break;
                    case DataHelper.PropertyDataType.Custom2:
                        break;
                    case DataHelper.PropertyDataType.Password:
                        break;
                    default:
                        break;
                }
                #endregion
            }

            return ControlsList;
        }

        public Dictionary<string, webControls.WebControl> [] CreateControlsForProduct_WithAllProperties(string EntityName, string CatalogName, ProductFamily productFamily, CatalogItem catalogItem, System.Web.UI.WebControls.ListBox ListBoxControl )
        {
            Dictionary<string, webControls.WebControl> [] Dictionaries  =  new Dictionary<string, webControls.WebControl>[2];

            DataTable DefinitionProperties = CreateDefinitionProperties_ForProductDefinition(EntityName);
            DataTable InventoryProperties = CreateInventoryProperties_ForProductDefinition(CatalogName);

            // Adds the controls which will be used to fill Base & Extended Properties of the Product
            Dictionary<string, webControls.WebControl> ControlsList_BaseAndExtendedProperties = CreateControlsForProduct_ForBaseAndExtendedProperties(DefinitionProperties, productFamily, catalogItem);

            // Adds the controls which will be used to fill INVENTORY Properties of the Product
            Dictionary<string, webControls.WebControl> ControlsList_InventoryProperties = CreateControlsForProduct_ForInventoryProperties(InventoryProperties, productFamily, catalogItem);

            // Adds the controls which will be used to fill CATEGORY RELATIONSHIPS of the Product
            CreateControlsForProduct_ForCategoryRelationships(productFamily, catalogItem, ref ListBoxControl);

            Dictionaries[0] = ControlsList_BaseAndExtendedProperties;
            Dictionaries[1] = ControlsList_InventoryProperties;

            return Dictionaries;
        }

        private void CreateControlsForProduct_ForCategoryRelationships(ProductFamily productFamily, CatalogItem catalogItem, ref System.Web.UI.WebControls.ListBox ListBoxControl)
        {
            if (null == ListBoxControl) return;
            
            if (null != catalogItem)
            {
                CategoryCollection Categories = catalogItem.ParentCategories;

                foreach (Category category in Categories)
                {
                    ListBoxControl.Items.Add(category.Name);
                }
            }
        }

        public Dictionary<string, WebControl>[] CreateControlsForCategory_WithAllProperties(string CategoryDefinition, string CatalogName, string CategoryName)
        {
            Dictionary<string, WebControl>[] DictionaryArray = new Dictionary<string, WebControl>[1];

            DictionaryArray[0] = CreateControls_ForCategory(CategoryDefinition, CatalogName, CategoryName);

            return DictionaryArray;
        }

        public Dictionary<string, webControls.WebControl> CreateControls_ForCategory(string CategoryDefinitionName, string CatalogName, string CategoryName)
        {
            Category category = CommerceCtx.GetCategory(CatalogName, CategoryName) as Category;

            Dictionary<string, webControls.WebControl> ControlsList = new Dictionary<string, webControls.WebControl>();

            #region DefinitionProperties table is begin prepared
            DataTable DefinitionProperties = DAL_ListEntities.ListSingleCategoryDefinitionsProperties(CategoryDefinitionName);
            DefinitionProperties.Columns.Add("PropertyDataType", Type.GetType("System.String"));
            DefinitionProperties.Columns.Add("PropertyDisplayName", Type.GetType("System.String"));
            DefinitionProperties.Columns.Add("IsRequired", Type.GetType("System.Boolean"));

            foreach (DataRow DefinitionPropertyRow in DefinitionProperties.Rows)
            {
                string PropertyName = DefinitionPropertyRow["PropertyName"].ToString();
                DataRow PropertyTypeRow = DAL_ListEntities.GetCategoryDefinitionPropertyAttributes(
                    String.Format("PropertyName='{0}'", PropertyName)
                );
                DefinitionPropertyRow["PropertyDataType"] = Convert.ToInt32(PropertyTypeRow["DataType"]);
                DefinitionPropertyRow["PropertyDisplayName"] = Convert.ToString(PropertyTypeRow["DisplayName_en-US"]);
                DefinitionPropertyRow["IsRequired"] = Convert.ToBoolean(PropertyTypeRow["IsRequired"]);
            }

            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.DisplayName, ref DefinitionProperties, true);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.CategoryName, ref DefinitionProperties, true);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.IsSearchable, ref DefinitionProperties, false);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.DefinitionName, ref DefinitionProperties, false);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.ListPrice, ref DefinitionProperties, false);

            DefinitionProperties.DefaultView.Sort = "PropertyDisplayName";
            DefinitionProperties = DefinitionProperties.DefaultView.ToTable();
            DefinitionProperties.DefaultView.Sort = "IsRequired DESC";
            DefinitionProperties = DefinitionProperties.DefaultView.ToTable();

            #endregion

            foreach (DataRow DefinitionPropertyRow in DefinitionProperties.Rows)
            {
                DataHelper.PropertyDataType propertyDataType = (DataHelper.PropertyDataType)Convert.ToInt32(DefinitionPropertyRow["PropertyDataType"]);
                string PropertyDefinitionName = Convert.ToString(DefinitionPropertyRow["PropertyName"]);
                string PropertyDisplayName = Convert.ToString(DefinitionPropertyRow["PropertyDisplayName"]);
                bool isRequired = Convert.ToBoolean(DefinitionPropertyRow["IsRequired"]);

                string PropertyName = PropertyDisplayName + " - " + PropertyDefinitionName;
                string ControlIdPrefix = "o_";
                if (isRequired) ControlIdPrefix = "r_";

                #region Create the control and add it to the control array
                webControls.WebControl webControl = null;

                switch (propertyDataType)
                {
                    case DataHelper.PropertyDataType.Integer:
                    case DataHelper.PropertyDataType.BigInteger:
                    case DataHelper.PropertyDataType.Float:
                    case DataHelper.PropertyDataType.Double:
                    case DataHelper.PropertyDataType.String:
                    case DataHelper.PropertyDataType.Text:
                    case DataHelper.PropertyDataType.DateTime:
                    case DataHelper.PropertyDataType.Date:
                    case DataHelper.PropertyDataType.Time:
                    case DataHelper.PropertyDataType.Currency:
                        webControls.TextBox txtTmp = new webControls.TextBox();
                        txtTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        webControl = txtTmp;
                        AssignDefaultValues(PropertyDefinitionName, ref webControl, category);

                        ControlsList.Add(PropertyName, txtTmp);
                        break;
                    case DataHelper.PropertyDataType.Boolean:
                        webControls.CheckBox chkTmp = new webControls.CheckBox();
                        chkTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        webControl = chkTmp;
                        AssignDefaultValues(PropertyDefinitionName, ref webControl, category);

                        ControlsList.Add(PropertyName, chkTmp);
                        break;
                    case DataHelper.PropertyDataType.Enumeration:
                        webControls.DropDownList ddlTmp = new webControls.DropDownList();
                        ddlTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        CatalogEnumerationProperty oCatalogProperty = (CatalogEnumerationProperty)CommerceCtx.GetProperty(PropertyDefinitionName);
                        CatalogEnumerationPropertyValuesDataSet.EnumerationPropertyValuesDataTable oEnumValuesTable = oCatalogProperty.EnumerationValues.EnumerationPropertyValues;
                        oEnumValuesTable.DefaultView.Sort = "Value";
                        foreach (DataRow enumValue in oEnumValuesTable.DefaultView.ToTable().Rows) {
                            ddlTmp.Items.Add(enumValue["Value"] as String);
                        }

                        webControl = ddlTmp;
                        AssignDefaultValues(PropertyDefinitionName, ref webControl, category);

                        ControlsList.Add(PropertyName, ddlTmp);
                        break;
                    case DataHelper.PropertyDataType.FilePath:
                    case DataHelper.PropertyDataType.MultiValue:
                    case DataHelper.PropertyDataType.CallbackEvent:
                    case DataHelper.PropertyDataType.MultilingualEnumeration:
                    case DataHelper.PropertyDataType.Guid:
                    case DataHelper.PropertyDataType.Custom1:
                    case DataHelper.PropertyDataType.Custom2:
                    case DataHelper.PropertyDataType.Password:
                        break;
                    default:
                        break;
                }

                #endregion
            }

            return ControlsList;
        }
        
        private Dictionary<string, webControls.WebControl> CreateControlsForProduct_ForBaseAndExtendedProperties(DataTable DefinitionProperties, ProductFamily productFamily, CatalogItem catalogItem)
        {
            Dictionary<string, webControls.WebControl> ControlsList = new Dictionary<string, webControls.WebControl>();

            #region Adds the controls which will be used to fill Base & Extended Properties of the Product

            foreach (DataRow DefinitionPropertyRow in DefinitionProperties.Rows)
            {
                DataHelper.PropertyDataType propertyDataType = (DataHelper.PropertyDataType)Convert.ToInt32(DefinitionPropertyRow["PropertyDataType"]);
                string PropertyDefinitionName = Convert.ToString(DefinitionPropertyRow["PropertyName"]);
                string PropertyDisplayName = Convert.ToString(DefinitionPropertyRow["PropertyDisplayName"]);
                bool isRequired = Convert.ToBoolean(DefinitionPropertyRow["IsRequired"]);
                
                string PropertyName = PropertyDisplayName + " - " + PropertyDefinitionName;
                string ControlIdPrefix = "o_";
                if (isRequired) ControlIdPrefix = "r_";

                WebControl tmpControl=null;

                switch (propertyDataType)
                {
                    case DataHelper.PropertyDataType.Integer:
                    case DataHelper.PropertyDataType.BigInteger:
                    case DataHelper.PropertyDataType.Float:
                    case DataHelper.PropertyDataType.Double:
                    case DataHelper.PropertyDataType.String:
                    case DataHelper.PropertyDataType.Text:
                    case DataHelper.PropertyDataType.DateTime:
                    case DataHelper.PropertyDataType.Date:
                    case DataHelper.PropertyDataType.Time:
                    case DataHelper.PropertyDataType.Currency:
                    case DataHelper.PropertyDataType.MultiValue:
                    case DataHelper.PropertyDataType.Custom1:
                    case DataHelper.PropertyDataType.Custom2:
                    case DataHelper.PropertyDataType.MultilingualEnumeration:
                        webControls.TextBox txtTmp = new webControls.TextBox();
                        txtTmp.ID = ControlIdPrefix + PropertyDefinitionName;
                        if (PropertyDefinitionName == "DefinitionName")
                        {   // Finds the DefinitionName of the product.
                            DataRow [] dataRows = DefinitionProperties.Select("definitionName<>'CUSTOM_ADDED'");
                            if(dataRows.Length>0) {
                                txtTmp.Text = DefinitionProperties.Select("definitionName<>'CUSTOM_ADDED'")[0]["DefinitionName"] as String;
                                txtTmp.Enabled = false;
                            }
                        }

                        tmpControl = txtTmp;
                        AssignDefaultValues(PropertyDefinitionName, ref tmpControl, productFamily, catalogItem);

                        ControlsList.Add(PropertyName, txtTmp);
                        break;
                    case DataHelper.PropertyDataType.Boolean:
                        webControls.CheckBox chkTmp = new webControls.CheckBox();
                        chkTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        tmpControl = chkTmp;
                        AssignDefaultValues(PropertyDefinitionName, ref tmpControl, productFamily, catalogItem);

                        ControlsList.Add(PropertyName, chkTmp);
                        break;
                    case DataHelper.PropertyDataType.Enumeration:
                        webControls.DropDownList ddlTmp = new webControls.DropDownList();
                        ddlTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        CatalogEnumerationProperty oCatalogProperty = (CatalogEnumerationProperty)CommerceCtx.GetProperty(PropertyDefinitionName);
                        CatalogEnumerationPropertyValuesDataSet.EnumerationPropertyValuesDataTable oEnumerationPropertyValues = oCatalogProperty.EnumerationValues.EnumerationPropertyValues;
                        oEnumerationPropertyValues.DefaultView.Sort = "Value";
                        foreach (DataRow enumValue in oEnumerationPropertyValues.DefaultView.ToTable().Rows)
                        {
                            ddlTmp.Items.Add(enumValue["Value"] as String);
                        }

                        tmpControl = ddlTmp;
                        AssignDefaultValues(PropertyDefinitionName, ref tmpControl, productFamily, catalogItem);

                        ControlsList.Add(PropertyName, ddlTmp);
                        break;
                    case DataHelper.PropertyDataType.FilePath:
                    case DataHelper.PropertyDataType.CallbackEvent:
                    case DataHelper.PropertyDataType.Guid:
                    case DataHelper.PropertyDataType.Password:
                        break;
                    default:
                        break;
                }
            }

            #endregion

            return ControlsList;
        }

        private Dictionary<string, webControls.WebControl> CreateControlsForProduct_ForInventoryProperties(DataTable DefinitionProperties, ProductFamily productFamily, CatalogItem catalogItem)
        {
            Dictionary<string, webControls.WebControl> ControlsList = new Dictionary<string, webControls.WebControl>();

            #region Adds the controls which will be used to fill Base & Extended Properties of the Product
            if (DefinitionProperties == null) return ControlsList;

            foreach (DataRow DefinitionPropertyRow in DefinitionProperties.Rows)
            {
                DataHelper.PropertyDataType propertyDataType = (DataHelper.PropertyDataType)Convert.ToInt32(DefinitionPropertyRow["DataType"]);
                string PropertyDefinitionName = Convert.ToString(DefinitionPropertyRow["PropertyName"]);
                string PropertyDisplayName = Convert.ToString(DefinitionPropertyRow["DisplayName_en-US"]);
                bool isRequired = Convert.ToBoolean(DefinitionPropertyRow["IsRequired"]);

                string PropertyName = PropertyDisplayName + " - " + PropertyDefinitionName;
                string ControlIdPrefix = "o_";
                if (isRequired) ControlIdPrefix = "r_";
                webControls.WebControl tmpWebControl = null;
                switch (propertyDataType)
                {
                    case DataHelper.PropertyDataType.Integer:
                    case DataHelper.PropertyDataType.BigInteger:
                    case DataHelper.PropertyDataType.Float:
                    case DataHelper.PropertyDataType.Double:
                    case DataHelper.PropertyDataType.String:
                    case DataHelper.PropertyDataType.Text:
                    case DataHelper.PropertyDataType.DateTime:
                    case DataHelper.PropertyDataType.Date:
                    case DataHelper.PropertyDataType.Time:
                    case DataHelper.PropertyDataType.Currency:
                    case DataHelper.PropertyDataType.MultiValue:
                    case DataHelper.PropertyDataType.Custom1:
                    case DataHelper.PropertyDataType.Custom2:
                    case DataHelper.PropertyDataType.MultilingualEnumeration:
                        webControls.TextBox txtTmp = new webControls.TextBox();
                        txtTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        tmpWebControl = txtTmp as webControls.WebControl;
                        AssignDefaultValues(PropertyDefinitionName, ref tmpWebControl, productFamily, catalogItem);
                        ControlsList.Add(PropertyName, txtTmp);
                        break;
                    case DataHelper.PropertyDataType.Boolean:
                        webControls.CheckBox chkTmp = new webControls.CheckBox();
                        chkTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        tmpWebControl = chkTmp as webControls.WebControl;
                        AssignDefaultValues(PropertyDefinitionName, ref tmpWebControl, productFamily, catalogItem);
                        ControlsList.Add(PropertyName, chkTmp);
                        break;
                    case DataHelper.PropertyDataType.Enumeration:
                        webControls.DropDownList ddlTmp = new webControls.DropDownList();
                        ddlTmp.ID = ControlIdPrefix + PropertyDefinitionName;

                        CatalogEnumerationProperty oCatalogProperty = (CatalogEnumerationProperty)CommerceCtx.GetProperty(PropertyDefinitionName);
                        CatalogEnumerationPropertyValuesDataSet.EnumerationPropertyValuesDataTable oEnumerationPropertyValues = oCatalogProperty.EnumerationValues.EnumerationPropertyValues;
                        oEnumerationPropertyValues.DefaultView.Sort = "Value";
                        foreach (DataRow enumValue in oEnumerationPropertyValues.DefaultView.ToTable().Rows)
                        {
                            ddlTmp.Items.Add(enumValue["Value"] as String);
                        }

                        tmpWebControl = ddlTmp as webControls.WebControl;
                        AssignDefaultValues(PropertyDefinitionName, ref tmpWebControl, productFamily, catalogItem);
                        ControlsList.Add(PropertyName, ddlTmp);
                        break;
                    case DataHelper.PropertyDataType.FilePath:
                    case DataHelper.PropertyDataType.CallbackEvent:
                    case DataHelper.PropertyDataType.Guid:
                    case DataHelper.PropertyDataType.Password:
                        break;
                    default:
                        break;
                }

            }

            #endregion

            return ControlsList;
        }

        private void AssignDefaultValues(string PropertyDefinitionName, ref webControls.WebControl ControlToProcess, ProductFamily productFamily, CatalogItem catalogItem)
        {
            webControls.DropDownList ddlTmp = ControlToProcess as webControls.DropDownList;
            webControls.CheckBox chkTmp = ControlToProcess as webControls.CheckBox;
            webControls.TextBox txtTmp = ControlToProcess as webControls.TextBox;

            switch (PropertyDefinitionName)
            {
                case "InventoryCatalogName":
                case "ProductCatalogName":
                case "SkuId":
                case "SkuVariantId":
                case "DefinitionName":
                case "LastRestocked":
                case "SkuLastModified":
                    txtTmp.Enabled = false;
                    break;
                default:
                    break;
            }
            if (null != txtTmp && null != catalogItem && catalogItem.Information.Tables.Count > 0 && catalogItem.Information.Tables[0].Rows.Count > 0)
            {
                txtTmp.Text = Convert.ToString( catalogItem.Information.Tables[0].Rows[0][PropertyDefinitionName] );
            }

            if (null != ddlTmp && null != catalogItem && catalogItem.Information.Tables.Count > 0 && catalogItem.Information.Tables[0].Rows.Count > 0)
            {
                ListItem li = ddlTmp.Items.FindByValue(Convert.ToString(catalogItem.Information.Tables[0].Rows[0][PropertyDefinitionName]));
                if (null != li) {
                    li.Selected = true;
                }
            }

            if (null != chkTmp && null != catalogItem && catalogItem.Information.Tables.Count > 0 && catalogItem.Information.Tables[0].Rows.Count > 0) {
                string tmp = catalogItem.Information.Tables[0].Rows[0][PropertyDefinitionName].ToString();
                if ( !String.IsNullOrEmpty(tmp) ) {
                    chkTmp.Checked = Convert.ToBoolean(tmp);
                }
            }

        }

        private void AssignDefaultValues(string PropertyDefinitionName, ref webControls.WebControl ControlToProcess, Category categoryObject)
        {
            webControls.DropDownList ddlTmp = ControlToProcess as webControls.DropDownList;
            webControls.CheckBox chkTmp = ControlToProcess as webControls.CheckBox;
            webControls.TextBox txtTmp = ControlToProcess as webControls.TextBox;

            switch (PropertyDefinitionName)
            {
                case "InventoryCatalogName":
                case "ProductCatalogName":
                case "SkuId":
                case "SkuVariantId":
                case "DefinitionName":
                    // txtTmp.Text = "DefaultCategory";
                    txtTmp.Enabled = false;
                    break;
                default:
                    break;
            }
            if (null != txtTmp && null!=categoryObject && categoryObject.Information.Tables.Count > 0 && categoryObject.Information.Tables[0].Rows.Count>0)
            {
                txtTmp.Text = categoryObject.Information.Tables[0].Rows[0][PropertyDefinitionName] as String;
            }
        }

        private DataTable CreateDefinitionProperties_ForProductDefinition(string EntityName)
        {
            // DataTable DefinitionProperties = DAL_ListEntities.ListSingleProductDefinitionsProperties(EntityName);
            DataTable DefinitionProperties = DAL_ListEntities.ListSingleProductDefinitionsProperties_AllProperties(EntityName);
            DefinitionProperties.Columns.Add("PropertyDataType", Type.GetType("System.String"));
            DefinitionProperties.Columns.Add("PropertyDisplayName", Type.GetType("System.String"));
            DefinitionProperties.Columns.Add("IsRequired", Type.GetType("System.Boolean"));

            foreach (DataRow DefinitionPropertyRow in DefinitionProperties.Rows)
            {
                // object[] tmp = DefinitionPropertyRow.ItemArray;
                string PropertyName = DefinitionPropertyRow["PropertyName"].ToString();
                DataRow PropertyTypeRow = DAL_ListEntities.GetProductDefinitionPropertyAttributes(
                    String.Format("PropertyName='{0}'", PropertyName)
                );
                DefinitionPropertyRow["PropertyDataType"] = Convert.ToInt32(PropertyTypeRow["DataType"]);
                DefinitionPropertyRow["PropertyDisplayName"] = Convert.ToString(PropertyTypeRow["DisplayName_en-US"]);
                DefinitionPropertyRow["IsRequired"] = Convert.ToBoolean(PropertyTypeRow["IsRequired"]);
            }

            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.ProductId, ref DefinitionProperties);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.DefinitionName, ref DefinitionProperties);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.DisplayName, ref DefinitionProperties);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.ListPrice, ref DefinitionProperties, true);

            DefinitionProperties.DefaultView.Sort = "PropertyDisplayName";
            DefinitionProperties = DefinitionProperties.DefaultView.ToTable();

            DefinitionProperties.DefaultView.Sort = "IsRequired DESC";
            DefinitionProperties = DefinitionProperties.DefaultView.ToTable();

            return DefinitionProperties;
        }

        /// <summary>
        /// InventoryProperties table is being prepared
        /// </summary>
        /// <returns>inventory catalog's properties datatable</returns>
        private DataTable CreateInventoryProperties_ForProductDefinition(string CatalogName)
        {
            // Add inventory fields
            InventoryCatalog inventoryCatalog = null;
            DataTable InventoryProperties = null;

            inventoryCatalog = CommerceCtx.InventoryContext.GetAssociatedInventoryCatalog(CatalogName);
            CatalogPropertiesDataSet inventorySkuProperties = CommerceCtx.GetEntityProperties(ExtensibleEntityType.InventorySku);
            if (inventorySkuProperties.Tables.Count > 0)
            {
                InventoryProperties = inventorySkuProperties.Tables[0].Clone();
            }

            DataRow[] rows = inventorySkuProperties.Tables[0].Select("1=1");
            for (int i = 0; i < rows.Length; i++)
            {
                InventoryProperties.ImportRow(rows[i]);
            }

            InventoryProperties.DefaultView.Sort = "DisplayName";
            InventoryProperties = InventoryProperties.DefaultView.ToTable();

            InventoryProperties.DefaultView.Sort = "IsRequired DESC";
            InventoryProperties = InventoryProperties.DefaultView.ToTable();

            return InventoryProperties;
        }

        private DataTable CreateDefinitionProperties_ForCategoryDefinition(string EntityName)
        {
            DataTable DefinitionProperties = DAL_ListEntities.ListSingleCategoryDefinitionsProperties(EntityName);
            DefinitionProperties.Columns.Add("PropertyDataType", Type.GetType("System.String"));
            DefinitionProperties.Columns.Add("PropertyDisplayName", Type.GetType("System.String"));
            DefinitionProperties.Columns.Add("IsRequired", Type.GetType("System.Boolean"));

            foreach (DataRow DefinitionPropertyRow in DefinitionProperties.Rows)
            {
                // object[] tmp = DefinitionPropertyRow.ItemArray;
                string PropertyName = DefinitionPropertyRow["PropertyName"].ToString();
                DataRow PropertyTypeRow = DAL_ListEntities.GetCategoryDefinitionPropertyAttributes(
                    String.Format("PropertyName='{0}'", PropertyName)
                );
                DefinitionPropertyRow["PropertyDataType"] = Convert.ToInt32(PropertyTypeRow["DataType"]);
                DefinitionPropertyRow["PropertyDisplayName"] = Convert.ToString(PropertyTypeRow["DisplayName_en-US"]);
                DefinitionPropertyRow["IsRequired"] = Convert.ToBoolean(PropertyTypeRow["IsRequired"]);
            }

            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.DisplayName, ref DefinitionProperties, true);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.CategoryName, ref DefinitionProperties, true);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.IsSearchable, ref DefinitionProperties, false);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.DefinitionName, ref DefinitionProperties, false);
            DAL_ListEntities.AddAttribute(CatalogItemsDataSetSchema.ListPrice, ref DefinitionProperties, false);

            DefinitionProperties.DefaultView.Sort = "PropertyDisplayName";
            DefinitionProperties = DefinitionProperties.DefaultView.ToTable();

            DefinitionProperties.DefaultView.Sort = "IsRequired DESC";
            DefinitionProperties = DefinitionProperties.DefaultView.ToTable();

            return DefinitionProperties;
        }

        public void CreateCategory_OnlyWithName(string CategoryDefinition, string CatalogName, string CategoryName)
        {
            try
            {
                BaseCatalog baseCat = CommerceCtx.GetCatalog(CatalogName, "tr-TR") as BaseCatalog;
                baseCat.CreateCategory(CategoryDefinition, CategoryName);
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
            }
        }



    }
}
